/*
Description of program:
Follows Pflueger and Viceira to adjust TIPS for liquidity
*/

clear 

local inputs_dir "C:\Users\yuxus\Dropbox\PROJECTS (ACTIVE)\Bond.Risk.Premia.Mitra.Xu\Data\PRIOR_TO_RFS_RR\combined_results_yu\data"

cd "C:\Users\yuxus\Dropbox\PROJECTS (ACTIVE)\Bond.Risk.Premia.Mitra.Xu\RFS manuscript and replication packet\replication\empirical results\Figure1_replication"

/* ----------------------------------------------------------------------------------------------------------------------------------------------------------
1. set up data
---------------------------------------------------------------------------------------------------------------------------------------------------------- */

cd "`outputs_dir'"

* gsw nominal bonds
use "`inputs_dir'\gsw_daily_ytm.dta"
// rename date dated

* gsw tips
merge 1:1 dated using "`inputs_dir'\gsw_daily_tips.dta"
keep if _merge==3
drop _merge

keep dated SVENY01-SVENY05 SVENY09 SVENY10 TIPSY02-TIPSY05 TIPSY09 TIPSY10

* 5- and 10-year swaps and on the run yields (from bloomberg)
merge 1:1 dated using "`inputs_dir'\bloomberg_yields_swaps_042021.dta"
keep if _merge!=2
drop _merge

* convert to monthly, using month end prices

gen D = day(dated)
gen M = month(dated)
gen Y = year(dated)
gen datem = ym(Y,M)
gen dateq = yq(Y,quarter(dated))
format datem %tm
format dateq %tq

egen max_D = max(D), by(datem)
keep if D==max_D

keep datem TIPSY* SVENY* USGG* USSWIT*

* monthly MICH inflation expectations
merge 1:1 datem using "`inputs_dir'\MICH.dta"
keep if _merge!=2
drop _merge

* Chicago Fed National Activies Index
merge 1:1 datem using "`inputs_dir'\CFNAI.dta"
keep if _merge!=2
drop _merge

* treasury/tips transactions
merge 1:1 datem using "`inputs_dir'\treasury transaction volume\Bonds_TransIndex_monthly.dta"
keep if _merge!=2
drop _merge

gen rel_trans_10y = log(TIPS_TransIndex_10y/Treas_TransIndex_10y)
gen rel_trans_5y = log(TIPS_TransIndex_5y/Treas_TransIndex_5y)


gen off_the_run_1y = SVENY01 - USGG12MIndex
gen off_the_run_2y = SVENY02 - USGG2YRIndex
gen off_the_run_3y = SVENY03 - USGG3YRIndex
gen off_the_run_5y = SVENY05 - USGG5YRIndex
gen off_the_run_4y = (off_the_run_3y + off_the_run_5y)/2

gen off_the_run_10y = SVENY10 - USGG10YR
gen off_the_run_7y = SVENY10 - USGG7YRIndex
gen off_the_run_9y = (1/3)*off_the_run_7y + (2/3)*off_the_run_10y

gen breakeven_2y = SVENY02 - TIPSY02
gen breakeven_3y = SVENY03 - TIPSY03
gen breakeven_4y = SVENY04 - TIPSY04
gen breakeven_5y = SVENY05 - TIPSY05
gen breakeven_9y = SVENY09 - TIPSY09
gen breakeven_10y = SVENY10 - TIPSY10

gen synthetic_cash_2y = USSWIT2Curncy - breakeven_2y
gen synthetic_cash_3y = USSWIT3Curncy - breakeven_3y
gen synthetic_cash_4y = USSWIT4Curncy - breakeven_4y
gen synthetic_cash_5y = USSWIT5Curncy - breakeven_5y
gen synthetic_cash_9y = USSWIT9Curncy - breakeven_9y
gen synthetic_cash_10y = USSWIT10Curncy - breakeven_10y

tsset datem

* detrend rel trans vol series
sort datem
gen time = _n

reg rel_trans_5y time
predict rel_trans_5y_detrend, resid
label variable rel_trans_5y_detrend "rel_trans_5y_detrend"

reg rel_trans_10y time
predict rel_trans_10y_detrend, resid
label variable rel_trans_10y_detrend "rel_trans_10y_detrend"

/* ----------------------------------------------------------------------------------------------------------------------------------------------------------
3. regs
---------------------------------------------------------------------------------------------------------------------------------------------------------- */

* local END_YR 2020
* local END_YR 2016

local NW_lags 12

local matlist 2 3 4 5 9 10

eststo clear

foreach m of local matlist{
	
	if `m'<=5{
		local RHS_YR 5
		local START_YR 2004
		local END_YR 2017
	}
	else if `m'>5{
		local RHS_YR 10
		local START_YR 1999
		local END_YR 2017
	}
	
	qui reg breakeven_`m'y MICH CFNAI off_the_run_`RHS_YR'y synthetic_cash_`m'y rel_trans_`RHS_YR'y_detrend if datem>=ym(`START_YR',1) & datem<=ym(`END_YR',12)
	local r2_temp = round(`e(r2)',.001)
	
	gen LIQADJ_`m'y = -_b[off_the_run_`RHS_YR'y]*off_the_run_`RHS_YR'y -_b[synthetic_cash_`m'y]*synthetic_cash_`m'y -_b[rel_trans_`RHS_YR'y_detrend]*rel_trans_`RHS_YR'y_detrend
	
	if `m'<=5{
		gen TIPS_LIQADJ_`m'y = TIPSY0`m' - LIQADJ_`m'y
	}
	else if `m'==9{
		gen TIPS_LIQADJ_`m'y = TIPSY0`m' - LIQADJ_`m'y
	}
	else if `m'==10{
		gen TIPS_LIQADJ_`m'y = TIPSY`m' - LIQADJ_`m'y
	}
	
	eststo: qui newey breakeven_`m'y MICH CFNAI off_the_run_`RHS_YR'y synthetic_cash_`m'y rel_trans_`RHS_YR'y_detrend if datem>=ym(`START_YR',1) & datem<=ym(`END_YR',12), lag(`NW_lags')
	qui estadd local display_NWlags "`NW_lags'"
	qui estadd local r_squared "`r2_temp'"
	
}

// esttab using "liq_adj_regs.tex", replace scalars("display_NWlags NW-lags" "r_squared R2") star(+ 0.10 * 0.05 ** 0.01 *** 0.001)

/* ----------------------------------------------------------------------------------------------------------------------------------------------------------
4. summary stats
---------------------------------------------------------------------------------------------------------------------------------------------------------- */

sum TIPSY* TIPS_LIQADJ* LIQADJ*

keep datem TIPS_LIQADJ*
save "liq_adj_tips.dta", replace